package com.market.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.entity.*;
import com.market.enums.RoleEnum;
import com.market.enums.StateEnum;
import com.market.service.BackstageService;
import com.market.utils.TokenUtil;
import com.market.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BackStageController
 * @Description TODO
 * @Author 1
 * @Date 2022/4/2 21:57
 * @Version 1.0
 */
@RestController
@RequestMapping("backstage")
public class BackStageController {
    @Autowired
    BackstageService backstageService;

    //后台人员登录
    @GetMapping("backstageLogin")
    public R backstageLogin(String account, String password, HttpServletResponse response) {
        Backstage backstage = backstageService.queryBackstageByAccount(account);
        if (backstage == null) {
            return new R(StateEnum.ACCOUNT_NOT_EXIST.getState(), "该账号不存在");
        }
        if (backstage.getPassword().equals(password)) {
            int backstageId = backstage.getId();
            int role = backstage.getRole();
            String roleName = "";
            switch (role) {
                case 1:
                    roleName = RoleEnum.MANAGER.getRoleName();
                    break;
                case 2:
                    roleName = RoleEnum.SORTER.getRoleName();
                    break;
                case 3:
                    roleName = RoleEnum.SALES_MAN.getRoleName();
                    break;
                case 4:
                    roleName = RoleEnum.CS_STAFF.getRoleName();
                    break;
                case 5:
                    roleName = RoleEnum.INTO_STORAGE.getRoleName();
                    break;
                case 6:
                    roleName = RoleEnum.BUYER.getRoleName();
                    break;
                default:
                    return new R(StateEnum.ROLE_NOT_EXIST.getState(), "该角色不存在");
            }
            String token = TokenUtil.getToken(backstageId, 60 * 60 * 1000, roleName);
            response.setHeader("token", token);
            return new R(StateEnum.LOGIN_SUCCESS.getState(), "后台登录成功", "token=" + token);
        } else {
            return new R(StateEnum.PASSWORD_ERROR.getState(), "密码错误");
        }
    }

    //管理员添加后台用户
    @PostMapping("admin/addBackStageUser")
    public R addBackStageUser(@RequestBody Backstage backstage, HttpServletRequest req) {
        if (backstageService.addBackStageUser(backstage)) {
            return new R(StateEnum.ADD_BACKSTAGE_SUCCESS.getState(), "添加后台用户成功");
        } else {
            return new R(StateEnum.ADD_BACKSTAGE_FAIL.getState(), "添加后台用户失败");
        }
    }

    //管理员根据id查询某个后台人员信息
    @GetMapping("admin/queryBackstage")
    public R queryBackstage(int backstageId) {
        Backstage backstage = backstageService.queryBackstage(backstageId);
        if (backstage != null) {
            return new R(StateEnum.ADD_BACKSTAGE_SUCCESS.getState(), "查询后台用户成功", backstage);
        } else {
            return new R(StateEnum.ADD_BACKSTAGE_FAIL.getState(), "查询后台用户失败");
        }
    }

    //根据token查询后台人员
    @GetMapping("/admin/queryBackstageByToken")
    public R queryBackstage(String token) {
        Map<String, Object> map = TokenUtil.parseToken(token);
        String backstageId = (String) map.get("id");
        int backstageId1 = Integer.valueOf(backstageId);
        Backstage backstage = backstageService.queryBackstage(backstageId1);
        if (backstage != null) {
            return new R(StateEnum.ADD_BACKSTAGE_SUCCESS.getState(), "查询后台用户成功", backstage);
        } else {
            return new R(StateEnum.ADD_BACKSTAGE_FAIL.getState(), "查询后台用户失败");
        }
    }

    //根据姓名和账号查询后台人员信息(找回密码)
    @GetMapping("admin/queryBackstageByNameAndAccount")
    public R queryBackstageByNameAndAccount(String name, String account) {
        Backstage backstage = backstageService.queryBackstageByNameAndAccount(name, account);
        if (backstage != null) {
            return new R(StateEnum.ADD_BACKSTAGE_SUCCESS.getState(), "查询后台用户成功", backstage);
        } else {
            return new R(StateEnum.ADD_BACKSTAGE_FAIL.getState(), "查询后台用户失败");
        }
    }

    //管理员根据id查询某个买家
    @GetMapping("admin/queryBuyerInfo")
    public R queryBuyerInfo(int buyerId) {
        Buyer buyer = backstageService.queryBuyerInfo(buyerId);
        if (buyer != null) {
            return new R(StateEnum.QUERY_BUYER_SUCCESS.getState(), "查询所有买家成功", buyer);
        } else {
            return new R(StateEnum.QUERY_BUYER_FAIL.getState(), "查询所有买家失败");
        }
    }

    //管理员查询所有后台人员(除管理和超管)
    @GetMapping("admin/queryAllBackstage")
    public R queryAllBackstage(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Backstage> backstages = backstageService.queryAllBackstage();
        if (backstages.size() != 0) {
            return new R(StateEnum.QUERY_BACKSTAGE_SUCCESS.getState(), "查询所有后台人员成功", new PageInfo<>(backstages));
        } else {
            return new R(StateEnum.QUERY_BACKSTAGE_FAIL.getState(), "查询所有后台人员失败");
        }
    }


    //管理员查看所有买家
    @GetMapping("admin/queryAllBuyer")
    public R queryAllBuyer(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Buyer> buyers = backstageService.queryAllBuyer();
        if (buyers.size() != 0) {
            return new R(StateEnum.QUERY_BUYER_SUCCESS.getState(), "查询买家成功", new PageInfo<>(buyers));
        } else {
            return new R(StateEnum.QUERY_BUYER_FAIL.getState(), "查询买家失败");
        }
    }

    //管理员锁定买家
    @GetMapping("admin/lockBuyer")
    public R lockBuyer(int buyerId) {
        if (backstageService.lockBuyer(buyerId)) {
            return new R(StateEnum.LOCK_BUYER_SUCCESS.getState(), "锁定买家成功");
        } else {
            return new R(StateEnum.LOCK_BUYER_FAIL.getState(), "锁定买家失败");
        }
    }

    //管理员解锁买家
    @GetMapping("admin/unLockBuyer")
    public R unLockBuyer(int buyerId) {
        if (backstageService.unLockBuyer(buyerId)) {
            return new R(StateEnum.UNLOCK_BUYER_SUCCESS.getState(), "解锁买家成功");
        } else {
            return new R(StateEnum.UNLOCK_BUYER_FAIL.getState(), "解锁买家失败");
        }
    }

    //管理员锁定后台人员
    @GetMapping("admin/lockBackstage")
    public R lockBackstage(int backstageId) {
        if (backstageService.lockBackstage(backstageId)) {
            return new R(StateEnum.LOCK_BACKSTAGE_SUCCESS.getState(), "锁定后台人员成功");
        } else {
            return new R(StateEnum.LOCK_BACKSTAGE_FAIL.getState(), "锁定后台人员失败");
        }
    }

    //管理员解锁后台人员
    @GetMapping("admin/unLockBackstage")
    public R unLockBackstage(int backstageId) {
        if (backstageService.unLockBackstage(backstageId)) {
            return new R(StateEnum.UNLOCK_BACKSTAGE_SUCCESS.getState(), "解锁后台人员成功");
        } else {
            return new R(StateEnum.UNLOCK_BACKSTAGE_FAIL.getState(), "解锁后台人员失败");
        }
    }

    //管理员修改后台人员权限
    @GetMapping("admin/updateRole")
    public R updateRole(int backstageId, int newRole) {
        if (backstageService.updateRole(backstageId, newRole)) {
            return new R(StateEnum.UPDATE_BACKSTAGE_AUTHORITY_SUCCESS.getState(), "修改后台人员权限成功");
        } else {
            return new R(StateEnum.UPDATE_BACKSTAGE_AUTHORITY_FAIL.getState(), "修改后台人员权限失败");
        }
    }

    //管理员新增商品
    @PostMapping("admin/addGods")
    public R<List<Goods>> addGods(@RequestBody Goods goods) {
        if (backstageService.addGoods(goods)) {
            return new R<>(StateEnum.ADD_GOODS_SUCCESS.getState(), "商品新增成功");
        } else {
            return new R<>(StateEnum.ADD_GOODS_FAIL.getState(), "商品新增失败");
        }
    }

    //管理员上架商品
    @GetMapping("admin/shelveGoods")
    public R<List<Goods>> shelveGoods(@RequestParam("goodsId") int goodsId) {
        if (backstageService.shelveGoods(goodsId)) {
            return new R<>(StateEnum.SHELVE_GOODS_SUCCESS.getState(), "商品上架成功");
        } else {
            return new R<>(StateEnum.SHELVE_GOODS_FAIL.getState(), "商品上架失败");
        }
    }

    //管理员下架商品
    @GetMapping("admin/offShelveGoods")
    public R<List<Goods>> offShelveGoods(@RequestParam("goodsId") int goodsId) {
        if (backstageService.offShelveGoods(goodsId)) {
            return new R<>(StateEnum.OFF_SHELVE_GOODS_SUCCESS.getState(), "商品下架成功");
        } else {
            return new R<>(StateEnum.OFF_SHELVE_GOODS_FAIL.getState(), "商品下架失败");
        }
    }

    //管理员删除商品
    @GetMapping("admin/deleteGoods")
    public R<List<Goods>> deleteGoods(int goodsId) {
        if (backstageService.deleteGoods(goodsId)) {
            return new R<>(StateEnum.DELETE_GOODS_SUCCESS.getState(), "商品删除成功");
        } else {
            return new R<>(StateEnum.DELETE_GOODS_FAIL.getState(), "商品删除失败");
        }
    }

    //管理员删除后台人员
    @GetMapping("admin/deleteBackStage")
    public R<List<Goods>> deleteBackStage(int backId) {
        if (backstageService.delBackstage(backId)) {
            return new R<>(StateEnum.DELETE_BACKSTAGE_SUCCESS.getState(), "后台人员删除成功");
        } else {
            return new R<>(StateEnum.DELETE_BACKSTAGE_FAIL.getState(), "后台人员删除失败");
        }
    }

    //管理员根据商品id查询某个商品
    @GetMapping("admin/queryGoodsByGoodsId")
    public R queryGoodsByGoodsId(int goodsId) {
        Goods result = backstageService.queryGoodsByGoodsId(goodsId);
        if (result != null) {
            return new R(StateEnum.QUERY_GOODS_SUCCESS.getState(), "查询商品成功", result);
        } else {
            return new R(StateEnum.QUERY_GOODS_FAIL.getState(), "查询商品失败");
        }
    }

    //管理员根据订单编号查看某个订单
    @GetMapping("admin/queryOrder")
    public R queryOrder(String orderCode) {
        Order result = backstageService.queryOrderByOrderCode(orderCode);
        if (result != null) {
            return new R(StateEnum.QUERY_ORDER_SUCCESS.getState(), "查询订单成功", result);
        } else {
            return new R(StateEnum.QUERY_ORDER_FAIL.getState(), "查询订单失败");
        }
    }

    //管理员查看所有订单
    @GetMapping("admin/queryAllOrder")
    public R queryAllOrder(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Order> orders = backstageService.queryAllOrder();
        if (orders != null) {
            return new R(StateEnum.QUERY_ORDER_SUCCESS.getState(), "查询所有订单成功", new PageInfo<>(orders));
        } else {
            return new R(StateEnum.QUERY_ORDER_FAIL.getState(), "查询所有订单失败");
        }
    }

    //管理员添加商品类别
    @GetMapping("admin/addGoodsType")
    public R addGoodsType(String typeName) {
        if (backstageService.addGoodsType(typeName)) {
            return new R(StateEnum.ADD_GOODS_TYPE_SUCCESS.getState(), "添加商品类别成功");
        } else {
            return new R(StateEnum.ADD_GOODS_TYPE_FAIL.getState(), "添加商品类别失败");
        }
    }

    //管理员删除某个订单
    @GetMapping("admin/deleteOrder")
    public R deleteOrder(int orderId) {
        if (backstageService.deleteOrder(orderId)) {
            return new R(StateEnum.DELETE_ORDER_SUCCESS.getState(), "删除订单成功");
        } else {
            return new R(StateEnum.DELETE_ORDER_FAIL.getState(), "删除订单失败");
        }
    }

    //管理员查询今天的已付款的订单
    @GetMapping("admin/queryTodayAlreadyPaidOrder")
    public R queryTodayAlreadyPaidOrder(String createTime, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Order> orders = backstageService.queryTodayAlreadyPaidOrder(createTime);
        if (orders != null) {
            return new R(StateEnum.QUERY_ORDER_SUCCESS.getState(), "查询今天的已付款的订单成功", new PageInfo<>(orders));
        } else {
            return new R(StateEnum.QUERY_ORDER_FAIL.getState(), "查询今天的已付款的订单失败");
        }
    }

    //管理员查看某个买家的全部订单
    @GetMapping("queryOrderByBuyerId")
    public R queryOrderByBuyerId(int buyerId, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Order> orders = backstageService.queryOrderByBuyerId(buyerId);
        if (orders!=null) {
            return new R(StateEnum.QUERY_ORDER_SUCCESS.getState(), "查询买家全部订单成功", new PageInfo<>(orders));
        }else {
            return new R(StateEnum.QUERY_ORDER_FAIL.getState(), "查询买家的全部订单失败");
        }
    }

    //入库员商品入库
    @GetMapping("intoStorage/goodsIntoStorage")
    public R goodsIntoStorage(int goodsId, String backStageName) {
        if (backstageService.goodsIntoStorage(goodsId, backStageName)) {
            return new R(StateEnum.INTO_STORAGE_SUCCESS.getState(), "入库成功");
        } else {
            return new R(StateEnum.INTO_STORAGE_FAIL.getState(), "入库失败");
        }
    }

    //管理员查看某个买家资金流水
    @GetMapping("admin/queryBuyerCashFlow")
    public R queryBuyerCashFlow(int buyerId, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<BuyerCashFlow> result = backstageService.queryBuyerCashFlow(buyerId);
        if (result != null) {
            return new R(StateEnum.QUERY_BUYER_FLOW_SUCCESS.getState(), "查看某个买家资金流水成功", new PageInfo<>(result));
        } else {
            return new R(StateEnum.QUERY_BUYER_FLOW_FAIL.getState(), "查看某个买家资金流水失败");
        }
    }

    //管理员修改可销售数量
    @GetMapping("admin/updateGoodsSellNum")
    public R updateGoodsSellNum(int goodsId, int newSellNum) {
        if (backstageService.updateGoodsSellNums(goodsId, newSellNum)) {
            return new R(StateEnum.UPDATE_SELL_NUM_FAIL.getState(), "修改可销售数量成功");
        } else {
            return new R(StateEnum.UPDATE_SELL_NUM_FAIL.getState(), "修改可销售数量失败");
        }
    }

    //客服审核是否与小店合作
    @GetMapping("CSStaff/checkStore")
    public R checkStore(int storeId, int newState) {
        if (backstageService.checkStore(storeId, newState)) {
            return new R(StateEnum.CHECK_STORE_SUCCESS.getState(), "审核成功");
        } else {
            return new R(StateEnum.CHECK_STORE_FAIL.getState(), "审核失败");
        }
    }

    //管理员根据账号查看某个后台人员
    @GetMapping("admin/queryBackstageByAccount")
    public R queryBackstageByAccount(String account) {
        Backstage backstage = backstageService.queryBackstageByAccount(account);
        if (backstage != null) {
            return new R(StateEnum.QUERY_SEND_BACK_SUCCESS.getState(), "根据账号查看某个后台人员成功", backstage);
        } else {
            return new R(StateEnum.QUERY_SEND_BACK_SUCCESS.getState(), "根据账号查看某个后台人员失败");
        }
    }

    //管理员查询某个买家的所有退货单
    @GetMapping("admin/queryAllSendBack")
    public R queryAllSendBack(int buyerId) {
        List<SendBack> sendBacks = backstageService.queryAllSendBack(buyerId);
        if (sendBacks.size() != 0) {
            return new R(StateEnum.QUERY_SEND_BACK_SUCCESS.getState(), "查询某个买家的所有退货单成功", sendBacks);
        } else {
            return new R(StateEnum.QUERY_SEND_BACK_FAIL.getState(), "查询某个买家的所有退货单失败");
        }
    }


    //客服审核退货
    @GetMapping("CSStaff/checkSendBackGoods")
    public R checkSendBackGoods(int sendBackId, int state) {
        if (backstageService.checkSendBackGoods(sendBackId, state)) {
            return new R(StateEnum.CHECK_SEND_BACK_SUCCESS.getState(), "审核退货成功");
        } else {
            return new R(StateEnum.CHECK_SEND_BACK_FAIL.getState(), "审核退货失败");
        }
    }

    //分拣员打印某天分拣单
    @GetMapping("sorter/printTodaySortRecord")
    public R printTodaySortRecord(String day) {
        List<SortRecord> sortRecords = backstageService.printTodaySortRecord(day);
        if (sortRecords.size() != 0) {
            return new R(StateEnum.PRINT_SORT_ORDER_SUCCESS.getState(), "打印某天分拣单成功", sortRecords);
        } else {
            return new R(StateEnum.PRINT_SORT_ORDER_FAIL.getState(), "打印某天分拣单失败");
        }
    }

    //查找所有状态为2的后台人员
    @GetMapping("admin/queryBackstageByState")
    public R queryBackstageByState(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Backstage> backstages = backstageService.queryBackstageByState();
        if (backstages.size() != 0) {
            return new R(StateEnum.QUERY_BACKSTAGE_SUCCESS.getState(), "查找所有状态为2的后台人员成功", new PageInfo<>(backstages));
        } else {
            return new R(StateEnum.QUERY_BACKSTAGE_FAIL.getState(), "查找所有状态为2的后台人员失败");
        }
    }

}
